// import { createApp } from 'vue'
// import App from './App.vue'
// import router from './router/index'


// // 创建 Vue 应用        
// const app = createApp(App)
// app.use(router)
// app.mount('#app')
import { createApp } from 'vue';
import App from './App.vue';
import { createRouter, createWebHistory } from 'vue-router';
// import { renderWithQiankun } from 'qiankun';
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

let instance = null;
let router = null;

function render(props = {}) {
  const { container } = props;

  router = createRouter({
    history: createWebHistory(qiankunWindow.__POWERED_BY_QIANKUN__ ? '/sub-vue' : '/'),
    routes: [
      {
        path: '/',
        name: 'Home',
        component: () => import('./views/Home.vue'),
      },
    ],
  });

  instance = createApp(App);
  instance.use(router);
  instance.mount(container ? container.querySelector('#vue-root') : '#vue-root');
}

renderWithQiankun({
  mount(props) {
    render(props);
  },
  bootstrap() {
    console.log('Vue app bootstraped');
  },
  unmount() {
    instance.unmount();
    instance = null;
    router = null;
  },
});

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render();
}
